package com.goldplusgold.support.lib.manager.time;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static com.goldplusgold.support.lib.manager.time.TimeBean.APPOINT;
import static com.goldplusgold.support.lib.manager.time.TimeBean.DELAY;
import static com.goldplusgold.support.lib.manager.time.TimeBean.LOOP_APPOINT;
import static com.goldplusgold.support.lib.manager.time.TimeBean.LOOP_DELAY;


/**
 * Created by Administrator on 2017/7/18.
 */

public class TimeManager {

    public List<TimeBean> lists ;
    public List<Timer> timers ;

    private static TimeManager timeManager ;

    public static TimeManager getIntance(){
        if(timeManager == null){
            timeManager = new TimeManager();
        }
        return  timeManager ;
    }

    private TimeManager(){
        lists = new ArrayList<TimeBean>();
        timers = new ArrayList<Timer>();
    }

    public void addBean(TimeBean timeBean){
        lists.add(timeBean);
        Timer timer = new Timer();
        MyTimerTask timerTask = new MyTimerTask(timeBean);
        switch (timeBean.getState()){
            case DELAY:
                timer.schedule(timerTask , timeBean.getDelaytime());
                break;
            case APPOINT:
                timer.schedule(timerTask , timeBean.getDelaydate());
                break;
            case LOOP_DELAY:
                timer.schedule(timerTask , timeBean.getDelaytime() , timeBean.getPeriod());
                break;
            case LOOP_APPOINT:
                timer.schedule(timerTask , timeBean.getDelaydate() , timeBean.getPeriod());
                break;
        }

        timers.add(timer);

    }

    public void remove(TimeBean timeBean){
        cancel(timeBean);
        lists.remove(timeBean);
    }

    //取消指定 定时任务
    public void cancel(int position){
        if(position >=0 && position < timers.size()) {
            timers.get(position).cancel();
            timers.remove(position);
            lists.remove(position);
        }
    }

    //取消指定 定时任务
    public void cancel(TimeBean timeBean){
        int count = -1 ;
        if(lists.contains(timeBean)){
            for(int i = 0 ; i < timers.size() ; i++){
                if(lists.get(i) == timeBean){
                    timers.get(i).cancel();
                    count = i ;
                    break ;
                }
            }
            if(count != -1) {
                timers.remove(count);
            }
            lists.remove(timeBean);
        }

    }

    //取消全部定时任务
    public void allCancel(){
        for(int i = 0 ; i < timers.size() ; i++){
            timers.get(i).cancel();
        }
        timers.clear();
        lists.clear();
    }

    class MyTimerTask extends TimerTask {

        TimeBean bean ;

        public MyTimerTask(TimeBean timeBean) {
            bean = timeBean ;
        }


        @Override
        public void run() {
            bean.getTimerInterface().doSomeThing();
            if(bean.getState() == DELAY || bean.getState() == APPOINT){
                remove(bean);
            }
        }
    }

}
